/*
    axiosFunc 传入的接口
    query  传入的参数
*/
export function usePage(axiosFunc, query) {
	const loading = ref(false)
	const finished = ref(false)
	const refreshing = ref(false)
	const pageNum = ref(1)
	const pageSize = ref(10)
	const listData = ref([])
	const getList = async () => {
		const res = await axiosFunc(
			Object.assign(
				{
					pageNum: pageNum.value,
					pageSize: pageSize.value,
				},
				query
			)
		)

		if (refreshing.value) {
			refreshing.value = false
			listData.value = []
		}

		const list = res.list || []

		if (pageNum.value > 1) {
			listData.value = listData.value.concat(list)
		} else {
			listData.value = list
		}
		loading.value = false
		if (listData.value.length >= res.total) {
			finished.value = true
		} else {
			finished.value = false
		}
	}

	// 下拉刷新
	const refreshCallback = () => {
		finished.value = false
		loading.value = true
		pageNum.value = 1
		setTimeout(() => {
			getList()
		}, 300)
	}

	// 上拉加载
	const loadMore = async () => {
		pageNum.value += 1
		getList()
	}

	return {
		listData,
		loading,
		finished,
		refreshing,
		getList,
		refreshCallback,
		loadMore,
	}
}
